package springboot.study.project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springboot.study.project.common.Result;
import springboot.study.project.model.TempRecord;
import springboot.study.project.model.User;
import springboot.study.project.model.VisitRecord;
import springboot.study.project.service.TempService;
import springboot.study.project.service.UserService;
import springboot.study.project.util.common.ResultUtil;
import springboot.study.project.util.common.StringUtil;

import java.util.Map;

/**
 * @ClassName: TempController
 * @Description: 温度相关控制层
 * @Author: liu-hao
 * @Date: 2021-03-28 16:29
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/temp")
public class TempController {

    @Autowired
    TempService tempService;
    @Autowired
    UserService userService;

    /**
     * @Author: liu-hao
     * @Description: 体温每日记录
     * @Date: 2021/3/28 16:55
     * @Param: tempRecord
     * @return: springboot.study.project.common.Result
     **/
    @RequestMapping("/tempRecord")
    public Result tempRecord(@RequestBody TempRecord tempRecord) {
        // 校验参数
        if (StringUtil.isBlank(tempRecord.getStuId()) || StringUtil.isBlank(tempRecord.getStuTemp()) || StringUtil.isBlank(tempRecord.getSymptom1()) || StringUtil.isBlank(tempRecord.getSymptom1())) {
            return ResultUtil.failure("请填写完成信息再进行提交！");
        }
        // 执行更新或保存操作
        boolean result = tempService.addOrModifyTempRecord(tempRecord);
        if (!result) {
            return ResultUtil.failure("登记体温失败！");
        }
        return ResultUtil.success();
    }


    @RequestMapping("/visitRegister")
    public Result visitRegister(@RequestBody VisitRecord visitRecord) {
        if (StringUtil.isBlank(visitRecord.getStuId()) || StringUtil.isBlank(visitRecord.getDormitoryId()) || StringUtil.isBlank(visitRecord.getStuTemp())) {
            return ResultUtil.failure("访问信息填写不完全！");
        }
        // 校验访问学生是否存在
        User user = userService.findUserById(visitRecord.getStuId());
        if (user == null) {
            return ResultUtil.failure("访问登记失败，该学生不存在！");
        }
        // 执行访问等级记录
        boolean result = tempService.visitRegister(visitRecord);
        if (!result) {
            return ResultUtil.failure("访问登记失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/findTempRecord")
    public Map<String, Object> findTempRecord(@RequestParam(name = "stuName") String stuName,
                                              @RequestParam(name = "startTemp") String startTemp,
                                              @RequestParam(name = "endTemp") String endTemp,
                                              @RequestParam(name = "page", defaultValue = "1") int pageNum,
                                              @RequestParam(name = "limit", defaultValue = "10") int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        Map<String, Object> resultMap = tempService.findTempRecord(stuName, startTemp, endTemp, offset, pageSize);
        return resultMap;
    }

    @RequestMapping("/findVisitRecord")
    public Map<String, Object> findVisitRecord(@RequestParam(name = "stuName") String stuName,
                                             @RequestParam(name = "dormitoryId") String dormitoryId,
                                             @RequestParam(name = "page", defaultValue = "1") int pageNum,
                                             @RequestParam(name = "limit", defaultValue = "10") int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        Map<String, Object> resultMap = tempService.findVisitRecord(stuName, dormitoryId, offset, pageSize);
        return resultMap;
    }
}
